/**
 * Radio Story Cards - using RFID tags to track physical story cards in an agile/lean environment.
 *
 * See http://www.radiostorycards.com for more information.
 *
 *
 * Copyright (C) 2011, Ted M. Young
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.radiostorycards.domain.cards;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.radiostorycards.datetime.DateTimeProvider;
import com.radiostorycards.datetime.RealTimeDateTimeProvider;
import com.radiostorycards.domain.tags.RfidTag;
import org.joda.time.DateTime;

import java.util.List;

/**
 * Represents a story card that's been linked to a specific RFID Tag.
 *
 * @author Ted Young <tedyoung@gmail.com> @JitterTed
 */
public class RadioCard {
  private RfidTag _rfidTag;
  private String _jiraNumber = "";
  private List<CardState> _cardStates = Lists.newArrayList();
  private static DateTimeProvider _dateTimeProvider = new RealTimeDateTimeProvider();

  private RadioCard(RfidTag rfidTag) {
    _rfidTag = rfidTag;
  }

  public static RadioCard createWithTag(RfidTag rfidTag) {
    return new RadioCard(rfidTag);
  }

  public void linkToJira(String jiraNumber) {
    _jiraNumber = jiraNumber;
  }

  public RfidTag getRfidTag() {
    return _rfidTag;
  }

  public String getJiraNumber() {
    return _jiraNumber;
  }

  public void transitionToInitialState() {
    _cardStates.add(new CardState(_dateTimeProvider.currentDateTime()));
  }

  public Column currentColumn() {
    return currentCardState().getColumn();
  }

  public static void setDateTimeProvider(DateTimeProvider dateTimeProvider) {
    _dateTimeProvider = dateTimeProvider;
  }

  public DateTime dateTimeOfTransition() {
    return currentCardState().getDateTime();
  }

  private CardState currentCardState() {
    if (_cardStates.isEmpty()) {
      throw new IllegalStateException("There are no card states for this card.");
    }
    return _cardStates.get(_cardStates.size() - 1);
  }

  /**
   * Transitions this card to the next column, stamping the date with the current date/time.
   */
  public void transitionToNextState() {
    CardState cardState = currentCardState();
    CardState nextCardState = cardState.transitionToNextState(_dateTimeProvider.currentDateTime());
    assert nextCardState != null : "Next CardState must not be null";
    _cardStates.add(nextCardState);
  }

  public Column getNextState() {
    return currentCardState().getNextColumn();
  }

  /**
   * Transitions this card to the specified column, stamping the date with the current date/time.
   *
   * @param column the column to which this card is transitioning.
   */
  public void transitionTo(Column column) {
    _cardStates.add(new CardState(column, _dateTimeProvider.currentDateTime()));
  }

  /**
   * Transitions this card to the specified column, stamping the date with the specified date/time.
   *
   * @param column   the column to which this card is transitioning.
   * @param dateTime the date & time that the transition took place.
   */
  void transitionTo(Column column, DateTime dateTime) {
    _cardStates.add(new CardState(column, dateTime));
  }

  @SuppressWarnings({"RedundantIfStatement"})
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    RadioCard radioCard = (RadioCard) o;

    if (!_jiraNumber.equals(radioCard._jiraNumber)) return false;
    if (_rfidTag != null ? !_rfidTag.equals(radioCard._rfidTag) : radioCard._rfidTag != null) return false;

    return true;
  }

  @Override
  public int hashCode() {
    int result = _rfidTag != null ? _rfidTag.hashCode() : 0;
    result = 31 * result + _jiraNumber.hashCode();
    return result;
  }

  @Override
  public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("RadioCard");
    sb.append("{_rfidTag=").append(_rfidTag);
    sb.append(", _jiraNumber='").append(_jiraNumber).append('\'');
    sb.append(", _cardStates=").append(_cardStates);
    sb.append('}');
    return sb.toString();
  }

  public List<CardState> getCardStates() {
    return ImmutableList.copyOf(_cardStates);
  }
}
